<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="物品名称" prop="psGoodsTypeId">
        <el-input
          v-model="queryParams.params.psGoodsTypeName"
          placeholder="请输入物品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="单价" prop="psPrice">
        <el-input
          v-model="queryParams.psPrice"
          placeholder="请输入单价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="日期查询" prop="psGoodsId">
        <el-date-picker
          v-model="dateValue"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="物品种类" prop="pstType">
        <el-select
          v-model="queryParams.params.pstType"
          placeholder="请选择物品种类"
          clearable
          multiple
        >
          <el-option
            v-for="dict in dict.type.ps_goods_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['procurement-system:procure:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['procurement-system:procure:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['procurement-system:procure:remove']"
          >删除回退</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['procurement-system:procure:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="procureList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="物品名称" align="center" prop="psGoodsTypeId">
        <template #default="scope">
          <div>
            {{
              typeList.find((item) => item.id === scope.row.psGoodsTypeId)
                ? typeList.find((item) => item.id === scope.row.psGoodsTypeId)
                    .pstName
                : scope.row.psGoodsTypeId
            }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="采购金额" align="center" prop="psAmount" />
      <el-table-column label="采购数量" align="center" prop="psNum" />
      <el-table-column label="数量单位" align="center" prop="psUnit">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.ps_goods_unit"
            :value="scope.row.psUnit"
          />
        </template>
      </el-table-column>
      <el-table-column label="单价" align="center" prop="psPrice" />
      <el-table-column label="种类" align="center" prop="pstType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.ps_goods_type"
            :value="scope.row.pstType"
          />
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createByName" />
      <el-table-column label="入库时间" align="center" prop="createTime" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['procurement-system:procure:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['procurement-system:procure:remove']"
            >删除回退</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改采购记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="物品名称" prop="psGoodsTypeId">
          <el-select
          filterable
            v-model="form.psGoodsTypeId"
            placeholder="请选择物品名称"
            style="width: 100%"
            @change="handleGoodsChange"
          >
            <el-option
              v-for="dict in typeList"
              :key="dict.id"
              :label="dict.pstName"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item  label="数量单位" prop="psUnit">
          <el-select
          filterable
            v-model="form.psUnit"
            placeholder="请选择数量单位"
            style="width: 100%"
          >
            <el-option
              v-for="dict in dict.type.ps_goods_unit"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <!--        <el-form-item label="采购金额" prop="psAmount">
          <el-input-number :min="0" :precision="2" v-model="form.psAmount" placeholder="请输入采购金额" :controls="false" style="width: 100%" />
        </el-form-item>-->
        <el-form-item label="采购数量" prop="psNum">
          <el-input-number
            :min="0"
            v-model="form.psNum"
            placeholder="请输入采购数量"
            :controls="false"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="单价" prop="psPrice">
          <el-input-number
            :min="0"
            :precision="2"
            v-model="form.psPrice"
            placeholder="请输入单价"
            :controls="false"
            style="width: 100%"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listProcure,
  getProcure,
  delProcure,
  addProcure,
  updateProcure,
} from "@/api/procurement-system/procure";
import { listType } from "@/api/procurement-system/type";

export default {
  name: "Procure",
  dicts: ["ps_goods_unit", "ps_goods_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购记录表格数据
      procureList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        psGoodsTypeId: null,
        psAmount: null,
        psNum: null,
        psUnit: null,
        psPrice: null,
        pstType: null,
        params: {},
      },
      dateValue: "",
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        psGoodsTypeId: {
          required: true,
          message: "请选择物品名称",
          trigger: "blur",
        },
        psUnit: { required: true, message: "请选择数量单位", trigger: "blur" },
        psNum: { required: true, message: "请输入采购数量", trigger: "blur" },
      },
      typeList: [],
    };
  },
  created() {
    this.getList();
    this.getListType();
  },
  methods: {
    getListType() {
      listType({ pageNum: 1, pageSize: 9999 }).then((response) => {
        this.typeList = response.rows;
      });
    },
    /** 查询采购记录列表 */
    getList() {
      this.loading = true;
      if (
        this.queryParams.params.pstType &&
        this.queryParams.params.pstType.length === 0
      ) {
        delete this.queryParams.params.pstType;
      }
      listProcure(this.queryParams).then((response) => {
        this.procureList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        psGoodsTypeId: null,
        psAmount: null,
        psNum: null,
        psUnit: null,
        psPrice: null,
        pstType: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if (this.dateValue && this.dateValue.length > 0) {
        this.queryParams.params.createTimeStart =
          this.dateValue[0].toLocaleDateString();
        this.queryParams.params.createTimeEnd =
          this.dateValue[1].toLocaleDateString();
      } else {
        this.queryParams.params.createTimeStart = "";
        this.queryParams.params.createTimeEnd = "";
      }
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateValue = "";
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加采购记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getProcure(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改采购记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateProcure(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProcure(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除采购记录编号为"' + ids + '"的数据项？')
        .then(function () {
          return delProcure(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "procurement-system/procure/export",
        {
          ...this.queryParams,
        },
        `procure_${new Date().getTime()}.xlsx`
      );
    },
    handleGoodsChange(value) {
      if (value) {
        const obj = this.typeList.find((item) => item.id === value);
        this.form.pstType = obj.pstType;
      } else {
        this.form.pstType = "";
      }
    },
  },
};
</script>
